package hbtDAO;

import hbt.HibernateUtil;

import java.util.ArrayList;
import java.util.List;

import model.Categoria;
import model.Tema;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;


public class HibernateCategoriaDAO {
	private static HibernateCategoriaDAO instancia;
	
	private static SessionFactory sf;

	public static HibernateCategoriaDAO getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia=new HibernateCategoriaDAO();
		} 
		return instancia;
	}
	
	@SuppressWarnings("unchecked")
	public List<Categoria> CategoriasList(){
		Session session = sf.openSession();
		Query q = session.createQuery("from Categoria p");
		List<Categoria> listaDeCategorias = new ArrayList<Categoria>(q.list());
		session.close();
		return listaDeCategorias;
	}
	
	public void guardarUnCategoria(Categoria t){
		Session session = sf.openSession();
		session.beginTransaction();
		session.saveOrUpdate(t); 
		session.getTransaction().commit();
		session.close();		
	}	
	
	public void borrarCategoria(Categoria t){
		Session session = sf.openSession();
		session.beginTransaction();
		session.delete(t); 
		session.getTransaction().commit();
		session.close();
	}
	
	public Categoria obtenerCategoria(long idCategoria){
		Session session = sf.openSession();
		Query q = session.createQuery("FROM Categoria u WHERE u.idCategoria = :idCategoria").setString("idCategoria", String.valueOf(idCategoria));
		Categoria t = (Categoria) q.uniqueResult();
		session.close();
		return t;
	}
	
}